#!/usr/bin/env bash

# 定义支持的目标平台列表
declare -a platforms=(
    "linux/amd64"
    "windows/amd64"
    #"darwin/amd64"
    #"darwin/arm64"
)

# 编译函数：接收OS和ARCH参数
build_platform() {
    local os=$1
    local arch=$2
    local output_name="output/micro-${os}-${arch}"

    # 添加.exe扩展名（Windows平台）
    if [ "$os" = "windows" ]; then
        output_name="${output_name}.exe"
    fi

    echo "开始编译 ${os}/${arch} 平台..."
    # 根据目标平台设置源文件名称
    local src_file="micro"
    if [ "$os" = "windows" ]; then
        src_file="micro.exe"
    fi
    if make GOOS=$os GOARCH=$arch build; then
        mv "${src_file}" "${output_name}"
        echo "✅ ${output_name} 编译成功"
    else
        echo "❌ ${os}/${arch} 平台编译失败"
        exit 1
    fi
}

# 清理旧构建文件
clean_old_builds() {
    echo "清理旧构建文件..."
    if [ -d "output" ]; then
        rm -f output/micro-* 2>/dev/null
    fi
}

# 主执行流程
main() {
    clean_old_builds
    mkdir -p output
    for platform in "${platforms[@]}"; do
        os=$(echo $platform | cut -d'/' -f1)
        arch=$(echo $platform | cut -d'/' -f2)
        build_platform "$os" "$arch"
    done
    echo "🎉 所有平台编译完成"
}

# 启动主流程
main

